package com.ruoyi.web.context;

import com.ruoyi.core.entity.RequestHead;
import com.ruoyi.core.entity.TokenInfo;
import com.ruoyi.core.entity.UserContext;
import com.ruoyi.core.util.IdUtil;
import com.ruoyi.core.util.RequestContextUtil;
import com.ruoyi.web.threadLocal.ThreadCache;
import com.ruoyi.web.util.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class RequestContextHolder {

    public static RequestContext currentRequestContext() {
        return ThreadCache.getRequestContext();
    }

    public static RequestContext initRequestContext() {

        RequestContext requestContext = new RequestContext();

        RequestHead head = new RequestHead();

        HttpServletRequest request = RequestContextUtil.getRequest();

        head.setToken(RequestContextUtil.getToken(request));
        head.setSign(RequestContextUtil.getSign(request));
        String requestId = RequestContextUtil.getRequestId(request);
        if (StringUtils.isEmpty(requestId)) {
            requestId = IdUtil.simpleUUID();
        }
        head.setRequestId(requestId);
        requestContext.setHead(head);

        if (!StringUtils.isEmpty(head.getToken())) {
            try {
                TokenInfo tokenInfo = JwtUtil.getTokenInfo(head.getToken());
                UserContext user = new UserContext();
                BeanUtils.copyProperties(tokenInfo, user);
                requestContext.setUser(user);
            } catch (Exception e) {
                head.setToken(null);
            }
        }

        MDC.put("trackId", requestId);

        return requestContext;
    }


    public static RequestHead currentRequestHead() {
        RequestContext requestContext = currentRequestContext();
        return requestContext == null ? null : requestContext.getHead();
    }

    public static String currentToken() {
        RequestHead requestHead = currentRequestHead();
        return requestHead == null ? null : requestHead.getToken();
    }


    public static UserContext currentUserContext() {
        RequestContext requestContext = currentRequestContext();
        return requestContext == null ? null : requestContext.getUser();
    }

    public static String currentUserId() {
        UserContext userContext = currentUserContext();
        return userContext == null ? null : userContext.getUserId();
    }

    public static String currentUserType() {
        UserContext userContext = currentUserContext();
        return userContext == null ? null : userContext.getUserType();
    }

    public static String currentUserName() {
        UserContext userContext = currentUserContext();
        return userContext == null ? null : userContext.getUserName();
    }

    public static String currentDeptId() {
        UserContext userContext = currentUserContext();
        return userContext == null ? null : userContext.getDeptId();
    }


}
